﻿using System.IdentityModel.Tokens.Jwt;
using System.Reflection;

namespace Zero.WebAPI.Framework.Security.Model
{
    public class Identity
    {
        public string Name { get; set; } = "";

        public string ID { get; set; } = "";

        public string Account { get; set; } = "";

        public string DistrictCode { get; set; } = "";

        public string DistrictName { get; set; } = "";

        public string Department { get; set; } = "";

        public string DepartmentName { get; set; } = "";

        public string Office { get; set; } = "";

        public string OfficeName { get; set; } = "";

        public string HardwareKey { get; set; } = "";

        public bool NeedHardware { get; set; } = false;

        public string UserIP { get; set; } = "";

        public DateTime Validity { get; set; } = DateTime.MinValue;

        public DateTime PsdValidity { get; set; } = DateTime.MinValue;

        public bool IsLogin { get; set; } = false;

        public string OrganizationCode { get; set; } = "";

        public string SessionID { get; set; } = "";

        public static void SetUserInfo(Identity model,JwtSecurityToken jwtSecurityToken)
        {
            PropertyInfo[] properties = typeof(Identity).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (var property in properties)
            {
                if(jwtSecurityToken.Payload.TryGetValue(property.Name, out var value))
                {
                    property.SetValue(model, value);
                }
            }
        }
    }
}
